package com.crm.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.crm.entity.InvoiceStatus;
import com.crm.entity.Opportunity;
import com.crm.util.ApplicationContext;
import com.crm.util.Constants;

public class InvoiceStatusDAO {
	protected final Log logger = LogFactory.getLog(getClass());
	
	public  int insert(InvoiceStatus c) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
		     tx = session.beginTransaction();
			session.saveOrUpdate(c);
			tx.commit();
			return 0;
		}
		catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public  int update(InvoiceStatus c) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
		     tx = session.beginTransaction();
			session.update(c);
			tx.commit();
			return 0;
		}
		catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public  int delete(InvoiceStatus c) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
		     tx = session.beginTransaction();
			session.delete(c);
			tx.commit();
			return 0;
		}
		catch (Exception e) {
		     if (tx!=null) tx.rollback();
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public  List<InvoiceStatus> findAll(int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			 List<InvoiceStatus> custObjs=new ArrayList<InvoiceStatus>();
			 List objs=null;
			Query query=session.createQuery("from InvoiceStatus where business_id=:busid");
			query.setInteger("busid", busid);
			objs=query.list();
			for(Object o:objs){
				custObjs.add((InvoiceStatus)o);
			}
			return custObjs;
		}
		catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public  InvoiceStatus findById(int i,int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			String qrystr="From InvoiceStatus sp where sp.id=:sid and business_id=:busid";
			Query qry=session.createQuery(qrystr);
			qry.setInteger("sid", i);
			qry.setInteger("busid", busid);
			Object ob=qry.uniqueResult();
			InvoiceStatus s=(InvoiceStatus)ob;
			return s;
		}
		catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public  List<InvoiceStatus> getPendInv(int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
		     String qryStr="from InvoiceStatus s where s.status!='paid' and business_id=:busid order by so_due_date"; 
		     Query qry=session.createQuery(qryStr);
		     qry.setInteger("busid", busid);
		     List<InvoiceStatus> s=qry.list();
		     return s;
		}
		catch (Exception e) {
		     throw e;
		}
		finally {
		     session.close();
		}
	}
	public  List<InvoiceStatus> getAllInv(int busid) throws Exception {
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			String qryStr="from InvoiceStatus s where business_id=:busid order by so_due_date"; 
			Query qry=session.createQuery(qryStr);
			qry.setInteger("busid", busid);
			List<InvoiceStatus> s=qry.list();
			return s;
		}
		catch (Exception e) {
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  int insertList(List<InvoiceStatus> invoiceList) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		Transaction tx=null;
		try {
			tx = session.beginTransaction();
			for(InvoiceStatus inv:invoiceList){
				session.saveOrUpdate(inv);
			}
			tx.commit();
			return 0;
		}
		catch (Exception e) {
			if (tx!=null) tx.rollback();
			throw e;
		}
		finally {
			session.close();
		}
	}
	public  List<InvoiceStatus> invoiceAlert(int busid) throws Exception{
		Session session =  ApplicationContext.getHiberSession().openSession();
		try {
			
			List<InvoiceStatus> custObjs=new ArrayList<InvoiceStatus>();
			Date today=new Date();
			int day=1000*60*60*24*5;
			java.sql.Date stdate=new java.sql.Date(today.getTime());
			java.sql.Date enddate=new java.sql.Date(today.getTime()+day);
			
			System.out.println("todays date insql "+new java.sql.Date(today.getTime()+day));
			String querystr="From InvoiceStatus  where ((so_due_date>=:dudt and so_due_date<=:dudat) or status=:status or status=:closedstatus) and business_id=:busid order by so_due_date";
			Query query=session.createQuery(querystr);
			query.setDate("dudt", stdate);
			query.setDate("dudat", enddate);
			query.setInteger("busid", busid);
			query.setString("status",Constants.PENDING );
			query.setString("closedstatus",Constants.PENDING_90_10);
			custObjs=query.list();
			return custObjs;		
		}
		catch (Exception ex) {
		     throw ex;
		}
		finally {
		     session.close();
		}
	}


}
